﻿// ------------------------------------------------------------------------------------
//      Copyright (c) 2013 uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 1/3/2013 4:07:44 PM
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Client.WpfClient.Login
{
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Input;
    using CP.NLayer.Client.WpfClient.Common;
    using CP.NLayer.Models.Business;
    using CP.NLayer.Models.Entities;
    using CP.NLayer.Resources.UI;
    using CP.NLayer.Service.Contracts;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Unity;

    public class LoginViewModel : ViewModelBase
    {
        private CultureInfo _selectedCulture;
        private bool? _dialogResult;
        private IInteractionService _interactionService;

        public LoginViewModel(IInteractionService interactionService)
        {
            this._interactionService = interactionService;
            this.SupportedCultures = new ObservableCollection<CultureInfo>()
            {
                new CultureInfo("zh"),
                new CultureInfo("en")
            };
            this.SelectedCulture = SupportedCultures.FirstOrDefault();
            this.LoginModel = new LoginModel();
            this.RadBusy = new RadBusyModel();
            this.LoginCommand = new DelegateCommand(ExecuteLoginCommand);
        }

        public LoginModel LoginModel { get; set; }

        public RadBusyModel RadBusy { get; set; }

        public CultureInfo SelectedCulture
        {
            get
            {
                return _selectedCulture;
            }
            set
            {
                _selectedCulture = value;
                WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = _selectedCulture;
                RaisePropertyChanged(() => this.SelectedCulture);
            }
        }

        public bool? DialogResult
        {
            get
            {
                return _dialogResult;
            }
            set
            {
                if (_dialogResult != value)
                {
                    _dialogResult = value;
                    this.RaisePropertyChanged(() => this.DialogResult);
                }
            }
        }

        public ObservableCollection<CultureInfo> SupportedCultures { get; private set; }

        public ICommand LoginCommand { get; private set; }

        private void ExecuteLoginCommand()
        {
            if (!string.IsNullOrEmpty(LoginModel.GetValidationErrors()))
            {
                return;
            }

            User user = null;
            this.RadBusy.DoWork(
                () =>
                {
                    this.RadBusy.BusyContent = UiResources.OnLoginPrompt;
                    var userService = DependencyInjection.Container.Resolve<IUserService>();
                    user = userService.Login(LoginModel.UserName, LoginModel.Password);
                },
                () =>
                {
                    if (user != null)
                    {
                        GlobalObjects.CurrentUser = user;
                        this.DialogResult = true;
                    }
                    else
                    {
                        this._interactionService.ShowAlert(UiResources.LoginFailed, null);
                    }
                });
        }
    }
}